package com.tester.yaa.exam002.volatileThread;

import java.util.concurrent.locks.LockSupport;

/**
 * @Aduthor yinanan
 * @ClassName Thread1
 * @Date 2022/12/11 19:12
 * @Description 线程实现
 **/
public class Thread1 {

    static Test test = new Test();

    Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            LockSupport.park(); //阻塞thread1线程
            LockSupport.unpark(thread2);//唤醒线程thread2
        }
    }, "thread1");

    //
    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i <= 10; i++) {
                test.add(i);
                System.out.println("add Object to Container" + i);
                if (test.size() == 5) {
                    System.out.println("test = " + test.size());
                    LockSupport.unpark(thread1);//唤醒线程thread1
                    LockSupport.park(); //阻塞thread2线程
                }
            }
        }
    }, "thread2");
}
